<?php
error_reporting(0);
session_start();
require_once "lib/db.class.php";
require_once "lib/email.class.php";
$link = db::getInstance();
$link->conn->query('set names utf8');
extract($_POST);

if($_COOKIE['phone'] == null){
	echo 2;
	exit();
}

function guid() {
	if (function_exists('com_create_guid')){
		$uuid = com_create_guid();
	}else{
		mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
		$charid = strtoupper(md5(uniqid(rand(), true)));
		$hyphen = chr(45);
		$uuid = chr(123)
		.substr($charid, 0, 8).$hyphen
		.substr($charid, 8, 4).$hyphen
		.substr($charid,12, 4).$hyphen
		.substr($charid,16, 4).$hyphen
		.substr($charid,20,12)
		.chr(125);
	}
	$uuid = str_replace(array('-', '{', '}'), '', $uuid);
	return $uuid;
}
$fields="`id`, `name`, `company`, `email`, `createtime`";
$values="'".guid()."', '".$name."', '".$company."', '".$email."', '".date("Y-m-d H:i:s")."'";
$res = $link->insertQueryInfo('submit_info',$fields,$values);
$content = "<div style=\"width:800px;margin:20 auto;padding:0;line-height:20px;font-size:14px;background-color:#fff\"><div style=\"margin:0;padding:0;padding:15px 0;border-bottom:1px solid #F2F2F2;\"><table style=\"font-size:16px;line-height:26px;\"><tr><td width=\"90\" style=\"text-align:center;\">".
"<img src=\"http://www.g7.com.cn/templates/default/images/logo.png\" alt=\"\" width=\"80\" height=\"80\"></td><td style=\"color:#080808;\">尊敬的".$name."客户，<br>您好！</td></tr></table></div>".
"<div style=\"padding:20px 100px;\"><p style=\"margin:0;padding:25px 0;color:#333;font-size:16px;\">根据您的要求为您提供<span style=\"padding:0 5px;color:#FF007F;\">".$_COOKIE['from']."~".$_COOKIE['to']."</span>，".
"货重区间为<span style=\"padding:0 5px;color:#FF007F;\">".$_COOKIE['weight']."</span>的车型油耗明细：</p>";
$fields = 'carbrand,carstyle,FORMAT(inf,2) as inf,FORMAT(sup,2) as sup';
$tableName = 'youhao';
$where = "((start_city = '". $_COOKIE['from'] ."' and end_city = '" . $_COOKIE['to'] ."') or (start_city = '". $_COOKIE['to'] ."' and end_city = '" . $_COOKIE['from'] ."')) and weight = '".$_COOKIE['weight']."' order by mean asc";
$result = $link->select2($tableName,$where,$fields);
$content .= "<table style=\"border-collapse: collapse;\" cellpadding=\"20\"><thead><tr><th style=\"border: 1px solid #E1E1E2;color:#272727;\">车辆品牌</th><th style=\"border: 1px solid #E1E1E2;color:#272727;\">车型</th><th style=\"border: 1px solid #E1E1E2;color:#272727;\">油耗区间</th></tr></thead>";
for($i=0; $i < count($result); $i++){
	$content .= "<tbody><tr>";
	$content .= "<td style=\"border: 1px solid #E1E1E2;color:#939393;\">".$result[$i]['carbrand']."</td><td style=\"border: 1px solid #E1E1E2;color:#939393;\">".$result[$i]['carstyle']."</td><td style=\"border: 1px solid #E1E1E2;color:#939393;\">".$result[$i]['inf'].'~'.$result[$i]['sup']."</td>";
	$content .= "</tr></tbody>";
}
$content .="</table><p style=\"margin:30px 0 0 0;padding:0;font-size:16px;color:#939393;\">如果您有什么问题，可以通过这个邮箱联系我们。</p></div></div>";
$email_instance = new Email();
$status = $email_instance->job_sendEmail(array(
		'sendto'	=> $email,
		'subject'	=> '[G7]线路油耗查询结果',
		'copyto'	=> '',
		'meto'	=> '',
		'emailtext'	=> $content,
	));

$link->close();

echo $status;